/**
 * Sharing.java
 * Purpose: Controller
 *
 * @version 1.0 5/15/11
 * @author Islam Al-Aarag
 */
package controllers;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import models.Idea;
import models.Notification;
import models.Plan;
import models.SharedIdea;
import models.SharedPlan;
import models.SharedTopic;
import models.Topic;
import models.User;
import play.mvc.*;

public class Sharing extends Controller {
	/**
	 * allows a user to share an idea with some user.Given a message, ideaId and
	 * list of user names.It gets the user that shared the idea and creates
	 * instances to save the shared idea for each user.It also assures that the
	 * user is not blocked by the other one else it ignores the sharing.
	 * 
	 * @story C4S66
	 * @author Islam Al-Aarag
	 * @param message
	 *            the message a user wants to add with sharing the idea
	 * @param ideaId
	 *            the id of the idea the user wants to share
	 * @param ids
	 *            a String containing ids of users to share the idea with comma
	 *            separated
	 */
	public static void shareIdea(String message, long ideaId, String ids) {
		String userName = session.get("username");
		User sharer = null;
		if (userName != null)
			sharer = User.find("byUserName", userName).first();
		if (sharer != null) {
			Idea sharedIdea = Idea.findById(ideaId);
			String[] users = ids.split(",");
			for (String id : users) {
				User sharedWith = User.findById(Long.parseLong(id));
				if (sharedWith == null || sharedWith.isBlocked(sharer.id)) {
					continue;
				} else {
					List<User> toBeNotified = new ArrayList<User>();
					toBeNotified.add(sharedWith);
					new Notification("has shared an idea on your wall.", 0, 15,
							sharer, toBeNotified).save();
					new SharedIdea(message, sharer, sharedWith, sharedIdea)
							.save();
				}
			}
		}
	}

	/**
	 * allows a user to share an topic with some user.Given a message, topicId
	 * and list of user names.It gets the user that shared the topic and creates
	 * instances to save the shared topic for each user.It also assures that a
	 * user is not blocked by the other one else it ignores the sharing.
	 * 
	 * @story C4S70
	 * @author Islam Al-Aarag
	 * @param message
	 *            the message a user wants to add with sharing the topic
	 * @param topicId
	 *            the id of the topic the user wants to share
	 * @param ids
	 *            a String containing ids of users to share the topic with comma
	 *            separated
	 */
	public static void shareTopic(String message, long topicId, String ids) {
		String userName = session.get("username");
		User sharer = null;
		if (userName != null)
			sharer = User.find("byUserName", userName).first();
		if (sharer != null) {
			Topic sharedTopic = Topic.findById(topicId);
			String[] users = ids.split(",");
			for (String id : users) {
				User sharedWith = User.findById(Long.parseLong(id));
				if (sharedWith == null || sharedWith.isBlocked(sharer.id)) {
					continue;
				} else {
					List<User> toBeNotified = new ArrayList<User>();
					toBeNotified.add(sharedWith);
					new Notification("has shared a topic on your wall.", 0, 15,
							sharer, toBeNotified).save();
					new SharedTopic(message, sharer, sharedWith, sharedTopic)
							.save();
				}
			}
		}
	}

	/**
	 * allows a user to share a plan with some user.Given a message, planId and
	 * list of user names.It gets the user that shared the plan and creates
	 * instances to save the shared plan for each user.It also assures that the
	 * user is not blocked by the other one else it ingores the sharing
	 * 
	 * @story C4S69
	 * @author Islam Al-Aarag
	 * @param message
	 *            the message a user wants to add with sharing the plan
	 * @param ideaId
	 *            the id of the plan the user wants to share
	 * @param ids
	 *            a String containing ids of users to share the plan with comma
	 *            separated
	 */
	public static void sharePlan(String message, long planId, String ids) {

		String userName = session.get("username");
		User sharer = null;
		if (userName != null)
			sharer = User.find("byUserName", userName).first();
		if (sharer != null) {
			Plan sharedPlan = Plan.findById(planId);
			String[] users = ids.split(",");
			for (String id : users) {
				User sharedWith = User.findById(Long.parseLong(id));
				if (sharedWith == null || sharedWith.isBlocked(sharer.id)) {
					continue;
				} else {
					List<User> toBeNotified = new ArrayList<User>();
					toBeNotified.add(sharedWith);
					new Notification("has shared a plan on your wall.", 0, 15,
							sharer, toBeNotified).save();
					new SharedPlan(message, sharer, sharedWith, sharedPlan)
							.save();
				}
			}
		}
	}

	/**
	 * given a searchKey fetches a list of users whose names include this
	 * searchKey and then render it as a JSON string
	 * 
	 * @author Islam Al-Aarag
	 * @story C4S66
	 * @param q
	 *            the searchKey that should be a part of the name of any user
	 *            fetched in the result
	 */
	public static void searchUser(String q) {
		List<User> result = User.find("byNameLike", "%" + q + "%").fetch();
		Gson gson = new Gson();
		JsonArray s = new JsonArray();
		for (User u : result) {
			JsonObject temp = new JsonObject();
			temp.addProperty("id", u.id);
			temp.addProperty("name", u.name);
			s.add(temp);
		}
		String results = gson.toJson(s);
		renderJSON(results);
	}
}